package net.orange.framework.logic;

import java.util.Collection;

import net.orange.architecture.db.Condition;
import net.orange.architecture.db.Conditions;
import net.orange.architecture.model.BaseValueObject;
import net.orange.architecture.process.BaseProcess;
import net.orange.console.schedule.JobStatus;
import net.orange.console.schedule.Schedule;
import net.orange.framework.model.Job;

/**
 * The job business logical process object which implement the IJobProcess
 * interface.
 */
public class JobProcess extends BaseProcess implements IJobProcess {


	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#insert(net.orange.architecture.vo.BaseValueObject)
	 */
	public boolean insert(BaseValueObject bvo) throws Exception {
		Job job = (Job) bvo;
		boolean b = super.insert(job);

		if (job.getStatus().equals(JobStatus.enabled.name()))
			Schedule.start(job);

		return b;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.IBaseProcess#update(net.orange.architecture.vo.BaseValueObject)
	 */
	public boolean update(BaseValueObject bvo) throws Exception {
		Job job = (Job) bvo;
		boolean b = super.update(job);

		if (job.getStatus().equals(JobStatus.enabled.name()))
			Schedule.start(job);
		else
			Schedule.stop(job);

		return b;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.process.BaseProcess#delete(net.orange.architecture.model.BaseValueObject)
	 */
	public boolean delete(BaseValueObject bvo) throws Exception {
		Job job = (Job) bvo;

		if (job != null)
			Schedule.stop(job);

		return super.delete(bvo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.console.job.process.IJobProcess#listAllEnabled()
	 */
	public Collection listAllEnabled() throws Exception {
		Conditions conditions = new Conditions();
		conditions.add(new Condition("status", JobStatus.enabled.name()));
		return dao.list(conditions);
	}
}